package com.yinhai.uid.utils;

import java.io.File;
import java.io.IOException;

/**
 * @author taojie
 * @Date: 2018/12/30 13:35
 * @Description:
 */
public class WorkIdUtils {

	public static final String WORKER_SPLIT = "-";

	/**
	 * 查找workId文件，根据前缀获取workid
	 * @param filePath
	 * @return
	 */
	public static long getWorkIdFromDisk(String filePath) {
		String workerId = null;
		File home = new File(filePath);
		if (home.exists() && home.isDirectory()) {
			File[] files = home.listFiles();
			workerId = files[0].getName();
			if (null != workerId) {
				return Long.parseLong(workerId);
			} else {
				throw new RuntimeException("从本地文件系统获取workerId失败");
			}
		}
		throw new RuntimeException("从本地文件系统获取workerId失败");
	}

	public static void writeWorkIdFile(String name) {
		File File = new File(name);
		File fileParent = File.getParentFile();
		if (!fileParent.exists()) {
			fileParent.mkdirs();
		}
		try {
			File.createNewFile();
		} catch (IOException e) {
			throw new RuntimeException("缓存workerID文件到本地文件系统失败");
		}
	}
}
